﻿#pragma execution_character_set("utf-8")
#ifndef QMYGRAPHICSVIEW_H
#define QMYGRAPHICSVIEW_H

#include <QGraphicsView>
#include <QGraphicsItem>
#include <QRubberBand>

//class QWheelEvent;
//class QKeyEvent;
class QMyGraphicsView:public QGraphicsView
{
    Q_OBJECT
public:
    explicit QMyGraphicsView(QWidget *parent = 0);

    // 平移速度
    void setTranslateSpeed(qreal speed);
    qreal translateSpeed() const;

    // 缩放增量
    void setZoomDelta(qreal delta);
    qreal zoomDelta() const;

protected:
    void contextMenuEvent(QContextMenuEvent *event) Q_DECL_OVERRIDE;
    void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE;
    void ResetItem();

public Q_SLOTS:
    void zoomIn();          // 放大
    void zoomOut();         // 缩小
    void translate(QPointF tran);
    void zoom(float scaleFactor);
    void updattran0(QPointF tran);

private:
    qreal m_translateSpeed; // 平移速度
    qreal m_zoomDelta;      // 缩放的增量
    bool is_mousePress;     // 按压标识
    bool is_mouseMove;      // 移动标识
    QPoint m_lastMousePos;  // 鼠标最后按下的位置
    qreal m_scale;          // 缩放值
    QPointF mouseDelta;     // 鼠标平移值
    QPointF sum_translate;  // 平移累计量
    qreal sum_rotation;     // 累计旋转量
    QGraphicsItem *item0;
    QRubberBand *rubberBand=nullptr;
    QPoint origin;
    QRect NewRect;

signals:
    void updateItemPos(QPointF &pos);   //发送鼠标位置的信号
    void transformview(QPointF &translation,qreal &rotation,qreal &scaling);
    void sendcrossitempos(QPointF);
    void sendcrossitemscale(qreal scale);
    void sendcrossitemselect(QRect);
};

#endif // QMYGRAPHICSVIEW_H
